<?php
namespace App\Http\Controllers\Backend\Auth;

use App\Exceptions\CommonException;
use App\Http\Controllers\Backend\BaseController;
use App\Http\Requests\Backend\Auth\AdminRequest;
use App\Repositories\PrtAdminRepository;
use App\Repositories\PrtAuthGroupRepository;
use Illuminate\Http\Request;
/**
 * 管理员列表
 * @author:yanxs<541139655@qq.com>
 * @version: 1.0
 * @date: 2021/3/12
 */
class AdminController extends BaseController
{
    protected $viewPath = 'backend.auth.admin';  //当前控制器所的view所在的目录
    protected $modules = 'auth';        //当前控制器所属模块
    protected $groupsList;              //角色列表
    public function __construct(PrtAdminRepository $Repository)
    {
        parent::__construct();
        $this->repositories = $Repository;

        $repoRole = app(PrtAuthGroupRepository::class);
        $this->groupsList = $repoRole->getGroupList();
    }

    /**
     * 进入功能页
     * @return mixed
     */
    public function index()
    {
        return view($this->viewPath.'.index',['groupList'=>$this->groupsList]);
    }

    /**
     * 数据列表
     * @param Request $request
     * @return array
     */
    protected function table(Request $request)
    {
        $request->merge(['extra' => ['groupList' => $this->groupsList]]);
        return parent::table($request);
    }

    /**
     * 表单显示
     * @param Request $request
     * @return mixed
     */
    public function form(Request $request)
    {
        //带入额外传数传入基类
        $request->merge(['extra' => ['groupList' => $this->groupsList]]);
        return parent::form($request); // TODO: Change the autogenerated stub
    }

    /**
     * 添加/编辑操作
     * @param AdminRequest $request
     * @return array
     */
    public function save(AdminRequest $request)
    {
        $ret = $this->repositories->save($request->all());
        if ($ret) {
            return $this->jsonSuccess([]);
        } else {
            return $this->jsonFailed('');
        }
    }

}
